package com.woniu108.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.carService.commos.dto.AdminInfoDto;
import com.woniu108.carService.commos.exception.admin.AdminException;
import com.woniu108.carService.commos.exception.admin.AdminExceptionCode;
import com.woniu108.carService.commos.exception.car.CarException;
import com.woniu108.carService.commos.exception.car.CarExceptionCode;
import org.springframework.stereotype.Service;
import com.woniu108.car.client.PersonClient;
import com.woniu108.car.mapper.CarDisplacementMapper;
import com.woniu108.car.model.CarDisplacement;
import com.woniu108.car.param.DeleteCarDisplacementParam;
import com.woniu108.car.param.InsertCarDisplacementParam;
import com.woniu108.car.param.UpdateCarDisplacementParam;
import com.woniu108.car.service.CarDisplacementService;

import javax.annotation.Resource;

/**
 * <p>
 * 车排量表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class CarDisplacementServiceImpl extends ServiceImpl<CarDisplacementMapper, CarDisplacement> implements CarDisplacementService {

    @Resource
    private CarDisplacementMapper carDisplacementMapper;
    @Resource
    private PersonClient personClient;

    /**
     * 判断管理员
     */
    private void isAdmin(String adminId) {
        // 检查 adminId 是否为 null
        if (adminId == null) {
            throw new AdminException(AdminExceptionCode.ADMIN_ID_NULL);
        }
        // 判断管理员权限
        AdminInfoDto adminInfo = personClient.getByAdminId(adminId).getData();
        if (adminInfo==null){
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_HAVE);
        }
        if (adminInfo.getAdminRoleId() != 1) {
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_PERMISSION);
        }
    }
    @Override
    public Boolean insertCarDisplacement(InsertCarDisplacementParam param) {
        //判断管理员
        isAdmin(param.getAdminId());
        QueryWrapper<CarDisplacement> wrapper = new QueryWrapper<>();
        wrapper.eq("displacement_num",param.getDisplacementNum());
        CarDisplacement selectOne = carDisplacementMapper.selectOne(wrapper);
        if (selectOne!=null){
            throw new CarException(CarExceptionCode.CAR_DISPLACEMENT_HAVE);
        }
        CarDisplacement carDisplacement = new CarDisplacement();
        carDisplacement.setDisplacementNum(param.getDisplacementNum());
        carDisplacementMapper.insert(carDisplacement);
        return true;
    }

    @Override
    public Boolean updateCarDisplacement(UpdateCarDisplacementParam param) {
        //判断管理员
        isAdmin(param.getAdminId());
        CarDisplacement carDisplacement = carDisplacementMapper.selectById(param.getDisId());
        if (carDisplacement==null){
            throw new CarException(CarExceptionCode.CAR_DISPLACEMENT_NOT_HAVE);
        }
        carDisplacement.setDisplacementNum(param.getDisplacementNum());
        carDisplacementMapper.updateById(carDisplacement);
        return true;
    }

    @Override
    public Boolean deleteCarDisplacement(DeleteCarDisplacementParam param) {
        isAdmin(param.getAdminId());
        CarDisplacement carDisplacement = carDisplacementMapper.selectById(param.getDisId());
        if (carDisplacement==null){
            throw new CarException(CarExceptionCode.CAR_DISPLACEMENT_NOT_HAVE);
        }
        carDisplacementMapper.deleteById(param.getDisId());
        return true;
    }
}
